package core {
	public class Field {
		private var _sizeX:int;
		private var _sizeY:int;

		private var f:Vector.<Vector.<SquareCell>>;
		private var _borderCell:SquareCell = new SquareCell();

		public function Field(sizeX:int, sizeY:int) {
			this._sizeX = sizeX;
			this._sizeY = sizeY;

			_borderCell.up = _borderCell;
			_borderCell.right = _borderCell;
			_borderCell.down = _borderCell;
			_borderCell.left = _borderCell;

			f = new Vector.<Vector.<SquareCell>>(sizeX, true);
			for (var x:int = 0; x < sizeX; x++) {
				f[x] = new Vector.<SquareCell>(sizeY, true);
				for (var y:int = 0; y < sizeY; y++) {
					f[x][y] = new SquareCell();
				}
			}

			for (var x:int = 0; x < sizeX; x++) {
				for (var y:int = 0; y < sizeY; y++) {
					f[x][y].up = getCellAt(x, y - 1);
					f[x][y].right = getCellAt(x + 1, y);
					f[x][y].down = getCellAt(x, y + 1);
					f[x][y].left = getCellAt(x - 1, y);
				}
			}
		}

		public function get sizeY():int {
			return _sizeY;
		}

		public function get sizeX():int {
			return _sizeX;
		}

		public function getCellAt(x:int, y:int):SquareCell {
			if (x < 0 || x + 1 >= sizeX || y < 0 || y + 1 >= sizeY) {
				return _borderCell;
			}
			return f[x][y];
		}

		public function get border():SquareCell {
			return _borderCell;
		}
	}
}
